/**
 * Created by Administrator on 2017/6/1 0001.
 */
export const KEY = "goodsdata";
export var valueObj =  {goodsid:0,count:0};
//实现数据增加
export  function setItem(value) {
    var jsonString=localStorage.getItem(KEY);
    jsonString= jsonString || "[]";
    var arr = JSON.parse(jsonString);
    arr.push(value);
    localStorage.setItem(KEY,JSON.stringify(arr));
}
//获取数据
export  function getItem() {
    var jsonString = localStorage.getItem(KEY);
    jsonString=jsonString || "[]";
    return JSON.parse(jsonString);
}
//移除数据
export  function remoteItem() {
    
}

export  function getgoodsObject() {
    var arr = getItem();
    var resObj = {};
    for(var i=0 ; i<arr.length;i++){
        var item = arr[i];
        if(!resObj[item.goodsid]){
            resObj[item.goodsid] = item.count;
        }else {
            var count=resObj[item.goodsid];
            resObj[item.goodsid] = count + item.count;
        }
    }
    return resObj;
}
//obj的格式：{goodsid:87,type:'add'}
export  function updateData(obj) {
    var arr=getItem();
    var count=1;
    if(obj.type == "add"){
        arr.push({goodsid:obj.goodsid,count:count})
    }else {
        for (var i=0;i<arr.length;i++){
            if(arr[i].goodsid == obj.goodsid){
                if(arr[i].count>1){
                    arr[i].count--;
                    break;
                }else {
                    arr.splice(i,1);
                    break;
                }
            }
        }
    }
}
    